package cn.zero.comtroller;

import cn.zero.entity.User;
import cn.zero.handler.UserFallBack;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope // 实时刷新远程配置
@RestController
public class UserController {

    @Value("${test}")
    private Long test;

    @GetMapping("/getUserById/{id}")
    @SentinelResource(value = "getUserById",
            blockHandler = "getUserByIdBlockHandler",
            blockHandlerClass = UserFallBack.class,
            fallback = "getUserByIdFallBack")
    public User getUserById(@PathVariable Long id){
        return User.builder().id(test).intro("嘻嘻嘻").name("cdc").build();
    }

    public static User getUserByIdFallBack(@PathVariable Long id,BlockException ex){
        return User.builder().id(-2L).intro("呵呵呵").name("cdc").build();
    }


}
